<?php

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {

        protected function _initFrontController()
        {
            new Zend_Application_Module_Autoloader(array(
                'namespace' => 'admin',
                'basePath' => APPLICATION_PATH . '/modules/admin',
            ));
            
            $front = Zend_Controller_Front::getInstance();

            $front->setControllerDirectory(array(
                'default' => APPLICATION_PATH . '/controllers',
                'admin' => APPLICATION_PATH . '/modules/admin/controllers'
            ));
            $front->registerPlugin(new Application_Plugin_SetApp());
            $front->registerPlugin(new Application_Plugin_SocialPlugin());
            $front->throwExceptions(TRUE);

            return $front;
        }

        protected function _initDB()
        {            
            if ($_SERVER["REMOTE_ADDR"] == "127.0.0.1")
                $dbConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/db-local.ini', "production");
            else
                $dbConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/db-server.ini', "production");

            $dbConfig = $dbConfig->resources->db;

            $dbAdapter = Zend_Db::factory($dbConfig->adapter, array(
                        'host' => $dbConfig->params->hostname,
                        'username' => $dbConfig->params->username,
                        'password' => $dbConfig->params->password,
                        'dbname' => $dbConfig->params->dbname
            ));

            $dbAdapter->getProfiler()->setEnabled(true);
            Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);

            Zend_Registry::set('db', $dbAdapter);

            Zend_Session::start();

            return $dbAdapter;
        }

        protected function _initRouter()
        {
            $frontController = Zend_Controller_Front::getInstance();
            $router = $frontController->getRouter();
            $route_array = array();

            $route_array["invite-to-event"] = new Zend_Controller_Router_Route('eventinvite/:event_id/:user_id/', array(
                'controller' => 'ajax',
                'action' => 'addtoeventdirect',
                'event_id' => '\d',
                'user_id' => '\d'
                    )
            );

            $route_array["event-detail"] = new Zend_Controller_Router_Route('eventdetail/:enc_event_id/', array(
                'controller' => 'event',
                'action' => 'eventdetail',
                'enc_event_id' => '\d'
                    )
            );

            $route_array["event-sorting"] = new Zend_Controller_Router_Route('sort/:sort_type/', array(
                'controller' => 'index',
                'action' => 'index',
                'sort_type' => '\d'
                    )
            );

            $route_array["update-social-username"] = new Zend_Controller_Router_Route('update-social-username/:social_network/:username/', array(
                'controller' => 'profile',
                'action' => 'updatesocialusername',
                'social_network' => '\d',
                'username' => '\d'
                    )
            );

            $route_array["public_profile_id"] = new Zend_Controller_Router_Route('profile/publicprofile/:user_id/', array(
                'controller' => 'profile',
                'action' => 'publicprofile',
                'user_id' => '\d'
                    )
            );

            $route_array["static_page"] = new Zend_Controller_Router_Route('static/:page', array(
                'controller' => 'static',
                'action' => 'index',
                'page' => '\d'
                    )
            );

            $route_array["friend_list"] = new Zend_Controller_Router_Route('friend-list/:user_id', array(
                'controller' => 'friend',
                'action' => 'friendlist',
                'user_id' => ''
                    )
            );

            $route_array["create-friend-group"] = new Zend_Controller_Router_Route('create-friend-group', array(
                'controller' => 'friend',
                'action' => 'creategroup'
                    )
            );

            $route_array["create-ad"] = new Zend_Controller_Router_Route('create-ad', array(
                'controller' => 'static',
                'action' => 'postad'
                    )
            );

            $route_array["delete-friend-group"] = new Zend_Controller_Router_Route('delete-friend-group/:group_id', array(
                'controller' => 'friend',
                'action' => 'deletegroup',
                'group_id' => '\d'
                    )
            );

            $route_array["edit-friend-group"] = new Zend_Controller_Router_Route('edit-friend-group/:group_id', array(
                'controller' => 'friend',
                'action' => 'editgroup',
                'group_id' => '\d'
                    )
            );

            $route_array["rate_user"] = new Zend_Controller_Router_Route('rate-user/:user_id', array(
                'controller' => 'ajax',
                'action' => 'rateuser',
                'user_id' => '\d'
                    )
            );

            if (!empty($route_array))
            {
                foreach ($route_array as $key => $value)
                {
                    $router->addRoute($key, $value);
                }
            }
        }

    }